package com.ml.mall.service.company.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.company.CompanyLogisticsInfoDto;
import com.ml.mall.bean.req.company.CompanyLogisticsInfoEditDto;
import com.ml.mall.bean.req.company.CompanyShippingDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.company.SysCompanyLogisticsInfo;
import com.ml.mall.entity.company.SysCompanyShipping;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.company.SysCompanyLogisticsInfoMapper;
import com.ml.mall.mapper.company.SysCompanyMapper;
import com.ml.mall.mapper.company.SysCompanyShippingMapper;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.company.SysCompanyLogisticsInfoService;
import com.ml.mall.utils.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysCompanyLogisticsInfoServiceImpl extends ServiceImpl<SysCompanyLogisticsInfoMapper, SysCompanyLogisticsInfo> implements SysCompanyLogisticsInfoService{

    @Autowired
    private SysCompanyLogisticsInfoMapper sysCompanyLogisticsInfoMapper;
    @Autowired
    private SysCompanyShippingMapper sysCompanyShippingMapper;

    @Autowired
    private SysCompanyMapper sysCompanyMapper;


    @Override
    public BigDecimal findDeliveryByCompanyId(Long companyId) {
        SysCompanyLogisticsInfo sysCompanyLogisticsInfo = sysCompanyLogisticsInfoMapper.selectByCompanyId(companyId);
        if(sysCompanyLogisticsInfo!=null&&sysCompanyLogisticsInfo.getDeliveryCharge()!=null){
            return sysCompanyLogisticsInfo.getDeliveryCharge();
        }
        return BigDecimal.ZERO;
    }

    @Override
    public void editSupplierLogistics(CompanyLogisticsInfoEditDto companyLogisticsInfoEditDto){
        SysUser currentUser = JwtUtil.getCurrentUser();
        editSupplierLogistics(companyLogisticsInfoEditDto.getCompanyLogistics(), companyLogisticsInfoEditDto.getCompanyLogisticsShipping(), currentUser.getCompanyId());
    }

    @Override
    @Transactional
    public void editSupplierLogistics(CompanyLogisticsInfoDto companyLogisticsInfoDto, CompanyShippingDto companyShippingDto, Long companyId) {
        SysUser currentUser = JwtUtil.getCurrentUser();
        //角色校验
        //只有供应商与管理员可编辑物流信息
        if(!(CommonConsts.ROLE_TYPE_SUPPLIER.equals(currentUser.getRoleType()) || CommonConsts.ROLE_TYPE_ADMIN.equals(currentUser.getRoleType()))){
            return;
        }
        //供应商只能修改本公司信息
        if(CommonConsts.ROLE_TYPE_SUPPLIER.equals(currentUser.getRoleType())){
            companyId = currentUser.getCompanyId();
        }

        //修改配送信息
        if(null != companyLogisticsInfoDto){
            SysCompanyLogisticsInfo sysCompanyLogisticsInfo = new SysCompanyLogisticsInfo();
            BeanUtils.copyProperties(companyLogisticsInfoDto, sysCompanyLogisticsInfo);
            sysCompanyLogisticsInfo.setCompanyId(companyId);

            QueryWrapper<SysCompanyLogisticsInfo> logisticsWrapper = new QueryWrapper<>();
            logisticsWrapper.eq("company_id", companyId);

            if(sysCompanyLogisticsInfoMapper.selectCount(logisticsWrapper) > 0){
                sysCompanyLogisticsInfoMapper.update(sysCompanyLogisticsInfo, logisticsWrapper);
            }else{
                sysCompanyLogisticsInfoMapper.insert(sysCompanyLogisticsInfo);
            }
        }

        //修改配送州属信息
        if(null != companyShippingDto){
            SysCompanyShipping sysCompanyShipping = new SysCompanyShipping();
            BeanUtils.copyProperties(companyShippingDto, sysCompanyShipping);
            if(null != companyShippingDto.getStatesArr()){
                sysCompanyShipping.setStatesArr(companyShippingDto.getStatesArr().toString());
            }
            sysCompanyShipping.setCompanyId(companyId);
            sysCompanyShipping.setState(CommonConsts.FIXED_NUM_ZERO);

            QueryWrapper<SysCompanyShipping> shippingWrapper = new QueryWrapper<>();
            shippingWrapper.eq("company_id", companyId);

            if(sysCompanyShippingMapper.selectCount(shippingWrapper) > 0){
                //更新公司的state_arr
                String str = sysCompanyShipping.getStatesArr();
                List<String> statesStrArr = (null == str ? new ArrayList<>() : StrUtils.getListByStr(str));
                String statesArr = "-";
                for(String stateId:statesStrArr){
                    statesArr = statesArr + stateId + "-";
                }
                sysCompanyMapper.updateStatesArrByCompanyId(companyId,statesArr);
                sysCompanyShippingMapper.update(sysCompanyShipping, shippingWrapper);
            }else{
                sysCompanyShippingMapper.insert(sysCompanyShipping);
            }
        }
    }

    @Override
    public BigDecimal findDeliveryMoqByCompanyId(Long companyId) {
        SysCompanyLogisticsInfo sysCompanyLogisticsInfo = sysCompanyLogisticsInfoMapper.selectByCompanyId(companyId);
        if(sysCompanyLogisticsInfo!=null&&sysCompanyLogisticsInfo.getDeliveryMoq()!=null){
            return sysCompanyLogisticsInfo.getDeliveryMoq();
        }
        return BigDecimal.ZERO;
    }

    @Override
    public SysCompanyLogisticsInfo findByCompanyId(Long companyId) {
        return sysCompanyLogisticsInfoMapper.selectByCompanyId(companyId);
    }

    /**
     * 判断物流信息是否完整
     * @param logisticsInfo
     * @return
     */
    @Override
    public boolean isCompletedLogisticsInfo(SysCompanyLogisticsInfo logisticsInfo) {
        if(null == logisticsInfo){
            return false;
        }
        if (null == logisticsInfo.getDeliveryCharge()
                || null == logisticsInfo.getDeliveryMoq()
                || null == logisticsInfo.getDeliveryTimeFrom()
                || null == logisticsInfo.getDeliveryTimeTo()) {
            return false;
        }
        return true;
    }

    /**
     * 检查物流配送信息
     * @param logisticsInfo
     */
    @Override
    public void checkLogisticsInfo(CompanyLogisticsInfoDto logisticsInfo) {
        if(logisticsInfo != null){
            BigDecimal deliveryMoq = logisticsInfo.getDeliveryMoq();
            if(deliveryMoq != null && deliveryMoq.compareTo(BigDecimal.ZERO) < 0){
                throw new BizException(CommonEnum.COMPANY_DELIVERY_MOQ_NEGATIVE);
            }
            BigDecimal deliveryCharge = logisticsInfo.getDeliveryCharge();
            if(deliveryCharge != null && deliveryCharge.compareTo(BigDecimal.ZERO) < 0){
                throw new BizException(CommonEnum.COMPANY_DELIVERY_CHARGE_NEGATIVE);
            }
        }
    }

    @Override
    public void saveSupplierLogistics(CompanyLogisticsInfoDto companyLogisticsInfoDto, Long companyId) {
        //添加配送信息
        if(null != companyLogisticsInfoDto && null != companyId){
            SysCompanyLogisticsInfo sysCompanyLogisticsInfo = new SysCompanyLogisticsInfo();
            BeanUtils.copyProperties(companyLogisticsInfoDto, sysCompanyLogisticsInfo);
            sysCompanyLogisticsInfo.setCompanyId(companyId);
            sysCompanyLogisticsInfo.setState(CommonConsts.FIXED_NUM_ONE);
            sysCompanyLogisticsInfoMapper.insert(sysCompanyLogisticsInfo);
        }
    }
}
